package com.lqd.mall.system.controller;

import com.lqd.mall.basis.code.CommonCode;
import com.lqd.mall.basis.response.CommonResult;
import com.lqd.mall.basis.response.Result;
import com.lqd.mall.interaction.basis.vo.TableDataInfoVo;
import com.lqd.mall.interaction.system.SysUserEntity;
import com.lqd.mall.interaction.system.vo.LoginUserVo;
import com.lqd.mall.jpa.controller.BaseController;
import com.lqd.mall.log.annotation.Log;
import com.lqd.mall.log.enums.BusinessType;
import com.lqd.mall.security.annotation.InnerAuth;
import com.lqd.mall.system.SysUserLoginControllerApi;
import com.lqd.mall.system.service.ISysUserLoginService;
import com.lqd.mall.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Optional;

/**
 * @author 零起点
 * @create 2022-02-20 0:32
 */
@RestController
public class SysUserLoginController extends BaseController implements SysUserLoginControllerApi {

    @Autowired
    ISysUserLoginService sysUserLoginService;

    @Autowired
    ISysUserService sysUserService;

    @Override
    @InnerAuth
    public Result<LoginUserVo> getUserInfo(String username) {
        LoginUserVo userInfo = sysUserLoginService.getUserInfo(username);

        return new Result<>(userInfo == null ? CommonCode.FAIL : CommonCode.SUCCESS, userInfo);
    }

    @Override
    public Result<Map<String, Object>> getInfo() {

        Map<String, Object> info = sysUserLoginService.getInfo();
        return Result.SUCCESS(info);
    }

    @Override
    public Result<Map<String, Object>> profile() {
        return Result.SUCCESS(sysUserLoginService.profile());

    }

    @Override
    @Log(title = "修改用户基本信息", businessType = BusinessType.UPDATE)
    public CommonResult upateUserLogin(SysUserEntity entity) {
        Optional<Integer> optional = sysUserService.updateEntity(entity, "user_id");
        return result(optional.get());
    }

    @Override
    @Log(title = "修改用户密码", businessType = BusinessType.UPDATE)
    public CommonResult updatePwd(String oldPassword, String newPassword) {
        return result(sysUserLoginService.updatePwd(oldPassword,newPassword));
    }

    @Override
    public Result<TableDataInfoVo<SysUserEntity>> listEntity(SysUserEntity entity) {
        TableDataInfoVo<SysUserEntity> infoVo = sysUserService.list(entity);
        return Result.SUCCESS(infoVo);
    }

    @Override
    public Result<SysUserEntity> listEntity(Long userId) {
        SysUserEntity entity = sysUserService.selectById(userId);
        return Result.SUCCESS(entity);
    }
}
